module Tuscan
  module Iec60584
    module TypeK
      T90_RANGE = -270.0..1372.0
      EMF_RANGE = -5.892..54.887

      include Math

      extend self

      def emfr_unbound t90
        if t90 < 0.0
          Polynomial.new(
             0.000000000000e+00,  0.394501280250e-01,  0.236223735980e-04, -0.328589067840e-06,
            -0.499048287770e-08, -0.675090591730e-10, -0.574103274280e-12, -0.310888728940e-14,
            -0.104516093650e-16, -0.198892668780e-19, -0.163226974860e-22
          ).solve_for t90
        else
          Polynomial.new(
            -0.176004136860e-01,  0.389212049750e-01,  0.185587700320e-04, -0.994575928740e-07,
             0.318409457190e-09, -0.560728448890e-12,  0.560750590590e-15, -0.320207200030e-18, 
             0.971511471520e-22, -0.121047212750e-25 
          ).solve_for(t90) +
          0.1185976e+00 * exp(-0.1183432e-03 * (t90 - 0.1269686e+03)**2)
        end
      end

      def t90r_guess emf
        if emf < 0.0
          Polynomial.new(
             0.0000000e+00,  2.5173462e+01, -1.1662878e+00, -1.0833638e+00, -8.9773540e-01, 
            -3.7342377e-01, -8.6632643e-02, -1.0450598e-02, -5.1920577e-04
          ).solve_for emf
        elsif emf < 20.644
          Polynomial.new(
             0.000000e+00,   2.508355e+01,   7.860106e-02,  -2.503131e-01,   8.315270e-02,
            -1.228034e-02,   9.804036e-04,  -4.413030e-05,
             1.057734e-06,  -1.052755e-08
          ).solve_for emf
        else
          Polynomial.new(
            -1.318058e+02,   4.830222e+01,  -1.646031e+00,   5.464731e-02,  -9.650715e-04,
             8.802193e-06,  -3.110810e-08
          ).solve_for emf
        end
      end
    end
  end
end

